package com.longcheer.weibo.api;

import com.longcheer.weibo.util.WeiboConstants;
import com.longcheer.weibo.vo.Count;
import com.longcheer.weibo.vo.Status;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

public class StatusesApi {
    
    public static final int INVALID_INTEGER = -1;
    public static final int INVALID_LONG = -1;
    public static final float INVALID_FLOAT = (float)-0.0000001;
    
    public static List<Status> getFriendsTimeline(Context context, long maxId, int count) throws WeiboException {
        return getFriendsTimeline(context, INVALID_LONG, maxId, count, INVALID_LONG, INVALID_LONG, INVALID_LONG);
    }
    
    public static List<Status> getFriendsTimeline(Context context,  int count) throws WeiboException {
        return getFriendsTimeline(context, INVALID_LONG, INVALID_LONG, count, INVALID_LONG, INVALID_LONG, INVALID_LONG);
    }
    
    public static List<Status> getFriendsTimeline(Context context, long sinceId, long maxId, 
            int count, int page, int baseApp, int feature) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "statuses/friends_timeline.json";
        
        WeiboParameters bundle = new WeiboParameters();
        bundle.add("source", Weibo.APP_KEY);
        if (sinceId >= 0) {
            bundle.add("since_id", String.valueOf(sinceId));
        }
        if (maxId >= 0) {
            bundle.add("max_id", String.valueOf(maxId));
        }
        if (count >= 0) {
            bundle.add("count", String.valueOf(count));
        }
        if (page >= 0) {
            bundle.add("page", String.valueOf(page));
        }
        if (baseApp >= 0) {
            bundle.add("base_app", String.valueOf(baseApp));
        }
        if (feature >= 0) {
            bundle.add("feature", String.valueOf(feature));
        }
        
        Weibo weibo = Weibo.getInstance();
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        JSONArray statuses;
        try {
            statuses = new JSONArray(resp);
            List<Status> friendTimeline = new ArrayList<Status>();
            for (int i = 0; i < statuses.length(); ++i) {
                friendTimeline.add(new Status(statuses.getJSONObject(i)));
            }
            return friendTimeline;
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
    
    public static Count getRepostAndCommentCount(Context context, long statusId) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "statuses/counts.json";
        WeiboParameters bundle = new WeiboParameters();
        bundle.add("source", Weibo.APP_KEY);
        bundle.add("ids", String.valueOf(statusId));
        
        Weibo weibo = Weibo.getInstance();
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        Count c = new Count();
        try {
            JSONArray array = new JSONArray(resp);
            for (int i = 0; i < array.length(); ++i) {
                JSONObject json = array.getJSONObject(i);
                long id = json.getLong("id");
                if (id == statusId) {
                    c.comments = json.getInt("comments");
                    c.reposts = json.getInt("rt");
                    return c;
                }
            }
            return null;
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
    
    public static Status updateWeibo(Context context, String text, long repostId) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "statuses/update.json";
        WeiboParameters bundle = new WeiboParameters();
        bundle.add("source", Weibo.APP_KEY);
        bundle.add("status", text);
        if (repostId != INVALID_LONG) {
            bundle.add("in_reply_to_status_id", String.valueOf(repostId));
        }

        Weibo weibo = Weibo.getInstance();
        String resp = weibo.request(context, url, bundle, "POST", weibo.getAccessToken());
        try {
            JSONObject json = new JSONObject(resp);
            return new Status(json);
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
}
